





CREATE PROC blog_GenericGetPagedEntries_10
(
	@ItemCount int,
	@PostType int,
	@PostConfig int,
	@BlogID int,
	@PageIndex int,
	@PageSize int,
	@CategoryID int = null,
	@CategoryName nvarchar(100) = null,
	@StartDate datetime = null,
	@StopDate datetime = null,
	@CategoryType int = null,
	@BlogGroupID int = null,
	@Author nvarchar(100) = null
)
as

/*
	Generic Entry Collection Proc With Categories

	All possible combinations will be filter by PostTye, PostConfig, and BlogID

	# of records will be controlled rowcount

	Order of precidence:
		CategoryID
		CategoryName
		StartDate
		Default
*/

DECLARE @PageLowerBound int
DECLARE @PageUpperBound int
SET NOCOUNT ON
SET @PageLowerBound = @PageSize * @PageIndex - @PageSize
SET @PageUpperBound = @PageLowerBound + @PageSize + 1

Create Table #IDs
(
	TempID int IDENTITY (1, 1) NOT NULL,
	EntryID int not null
)

Insert #IDs (EntryID)
exec blog_GenericGetEntryIDs_10 @ItemCount, @PostType, @PostConfig, @BlogID, @CategoryID, @CategoryName, @StartDate, @StopDate,@CategoryType,@BlogGroupID,@Author
if(@BlogID is  null)
Begin
SELECT	
	bc.BlogID, bc.[ID], bc.Title, bc.DateAdded, 
	bc.[Text], bc.[Description],bc.SourceUrl, bc.PostType, 
	bc.Author, bc.Email, bc.SourceName, bc.DateUpdated, 
	bc.TitleUrl, bc.FeedbackCount,	bc.ParentID,
	bc.PostConfig,	bc.EntryName,	vc.WebCount,
	vc.AggCount,	vc.WebLastUpdated,vc.AggLastUpdated,bcc.Application
		
FROM  	blog_content bc with(nolock)
    	INNER JOIN #IDS tmp ON (bc.[ID] = tmp.EntryID)
    	inner join blog_Config bcc with(nolock) on bc.BlogID = bcc.BlogID
    	Left JOIN  blog_EntryViewCount vc with(nolock) ON (bc.[ID] = vc.EntryID )
WHERE 	tmp.TempID > @PageLowerBound 
		AND tmp.TempID < @PageUpperBound
ORDER BY tmp.TempID
End
else
Begin
SELECT	
	bc.BlogID, bc.[ID], bc.Title, bc.DateAdded, 
	bc.[Text], bc.[Description],bc.SourceUrl, bc.PostType, 
	bc.Author, bc.Email, bc.SourceName, bc.DateUpdated, 
	bc.TitleUrl, bc.FeedbackCount,	bc.ParentID,
	bc.PostConfig,	bc.EntryName,	vc.WebCount,
	vc.AggCount, vc.WebLastUpdated,vc.AggLastUpdated
		
FROM  	blog_content bc with(nolock)
    	INNER JOIN #IDS tmp ON (bc.[ID] = tmp.EntryID)
  		Left JOIN  blog_EntryViewCount vc with(nolock) ON (bc.[ID] = vc.EntryID and vc.BlogID = @BlogID)
WHERE 	bc.BlogID = @BlogID 
		AND tmp.TempID > @PageLowerBound 
		AND tmp.TempID < @PageUpperBound
ORDER BY tmp.TempID
End 



SELECT COUNT(*) as TotalRecords
FROM 	#IDS 


DROP TABLE #IDS

GO
